package com.example.enjoyfood

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import androidx.fragment.app.Fragment
import com.example.enjoyfood.Adapter.RecipesAdapter

class RecipesFragment : Fragment() {

    private lateinit var gridView: GridView

    private val recipes = listOf(
        Recipe("糖醋里脊", R.drawable.recipe1, "recipe1.txt", " http://example.com/video1"),
        Recipe("红烧茄子", R.drawable.recipe2, "recipe2.txt", " http://example.com/video2 "),
        Recipe("红烧牛肉面", R.drawable.recipe3, "recipe3.txt", "http://example.com/video3"),
        Recipe("煎饼果子", R.drawable.recipe4, "recipe4.txt", "http://example.com/video4"),
        Recipe("西蓝花虾仁", R.drawable.recipe5, "recipe5.txt", "http://example.com/video5"),
        Recipe("口味虾", R.drawable.recipe6, "recipe6.txt", "http://example.com/video6"),
        Recipe("羊肉泡馍", R.drawable.recipe7, "recipe7.txt", "http://example.com/video7"),
        Recipe("石斑鱼", R.drawable.recipe8, "recipe8.txt", "http://example.com/video8"),
        Recipe("糟粕醋火锅", R.drawable.recipe9, "recipe9.txt", "http://example.com/video9"),
        Recipe("肉夹馍", R.drawable.recipe10, "recipe10.txt", "http://example.com/video10"),
    )

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_recipes, container, false)

        gridView = view.findViewById(R.id.recipes_grid_view)
        val adapter = RecipesAdapter(requireContext(), recipes)
        gridView.adapter = adapter

        gridView.setOnItemClickListener { _, _, position, _ ->
            val selectedRecipe = recipes[position]
            val intent = Intent(requireContext(), RecipeDetailActivity::class.java)
            intent.putExtra("recipe_name", selectedRecipe.name)
            intent.putExtra("recipe_image", selectedRecipe.imageResId)
            intent.putExtra("recipe_file", selectedRecipe.recipeFileName)
            intent.putExtra("video_url", selectedRecipe.videoUrl)
            startActivity(intent)
        }

        return view
    }
}